package controller

import (
	"github.com/bear/wolf/service"
	"github.com/bear/wolf/utils"
	"github.com/gin-gonic/gin"
)

// GetContractBalanceHandler 处理查询合约余额请求
func GetContractBalance(c *gin.Context) {
	balance ,err := service.GetContractBalance()
	if err != nil {
		utils.Failed(c, "查询失败: "+err.Error())
		return
	}
	utils.Success(c,balance)
}

// TransferToContractHandler 处理向合约转账请求
func TransferToContractHandler(c *gin.Context) {
	var req struct{
		PrivKey string `json:"priv_key" binding:"required"` // 发送者私钥（实际项目需用钱包签名，避免明文传输）
		Amount  string `json:"amount" binding:"required"`  // 转账金额（ETH）
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Failed(c,"参数错误: "+err.Error())
		return
	}
		// 调用服务层转账
	txHash, err := service.TransferToContract(req.PrivKey, req.Amount)
	if err != nil {
		utils.Failed(c, "转账失败: "+err.Error())
		return
	}

	utils.Success(c, map[string]string{
		"tx_hash": txHash,
		"message": "转账交易已发送，等待确认",
	})
}